package com.youlai.system.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.pojo.entity.device.DeviceWarnSettingEntity;
import com.youlai.system.pojo.form.DeviceWarnSettingForm;
import com.youlai.system.pojo.query.DeviceWarnSettingQuery;
import com.youlai.system.pojo.vo.device.DeviceWarnSettingPageVO;
import com.youlai.system.service.impl.device.DeviceService;
import com.youlai.system.service.impl.device.DeviceWarnSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "10.设备预警设置接口")
@RestController
@RequestMapping("/api/v1/deviceWarnSetting")
@RequiredArgsConstructor
public class DeviceWarnSettingController {

    private final DeviceWarnSettingService deviceWarnSettingService;

    private final DeviceService deviceService;


    @PostMapping("/addOrUpdate")
    @Operation(summary = "添加或者更新预警", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Boolean> addOrUpdate(@RequestBody DeviceWarnSettingForm deviceWarnSettingForm) {

        if (deviceWarnSettingForm.getId() == null) {
            LambdaQueryWrapper<DeviceWarnSettingEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DeviceWarnSettingEntity::getDeviceId, deviceWarnSettingForm.getDeviceId());
            lambdaQueryWrapper.eq(DeviceWarnSettingEntity::getType, deviceWarnSettingForm);


            List<DeviceWarnSettingEntity> deviceWarnSettingEntityList = deviceWarnSettingService.list(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(deviceWarnSettingEntityList)) {
                deviceWarnSettingForm.setId(deviceWarnSettingEntityList.get(0).getId());
            }

        }
        DeviceWarnSettingEntity deviceWarnSettingEntity = new DeviceWarnSettingEntity();
        BeanUtils.copyProperties(deviceWarnSettingForm, deviceWarnSettingEntity);
        if (deviceWarnSettingForm.getId() != null) {
            deviceWarnSettingEntity.setCreateTime(new Date());
        }
        deviceWarnSettingService.saveOrUpdate(deviceWarnSettingEntity);

        return Result.success(Boolean.TRUE);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "预警详情", security = {@SecurityRequirement(name = "Authorization")})
    public Result<DeviceWarnSettingEntity> detail(@PathVariable Integer id) {
        DeviceWarnSettingEntity deviceWarnSettingEntity = deviceWarnSettingService.getById(id);
        return Result.success(deviceWarnSettingEntity);
    }

    @Operation(summary = "设备预警配置分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<DeviceWarnSettingPageVO> page(DeviceWarnSettingQuery queryParams) {

        LambdaQueryWrapper<DeviceWarnSettingEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryParams.getDeviceId() != null) {
            lambdaQueryWrapper.eq(DeviceWarnSettingEntity::getDeviceId, queryParams.getDeviceId());
        }
        if (queryParams.getType() != null) {
            lambdaQueryWrapper.eq(DeviceWarnSettingEntity::getType, queryParams.getType());
        }

        lambdaQueryWrapper.orderByDesc(DeviceWarnSettingEntity::getId);
        Page<DeviceWarnSettingEntity> page = new Page(queryParams.getPageNum(), queryParams.getPageSize());
        page = deviceWarnSettingService.page(page, lambdaQueryWrapper);


        IPage<DeviceWarnSettingPageVO> warnSettingPageVOIPage = new Page<>();
        warnSettingPageVOIPage.setPages(page.getPages());
        warnSettingPageVOIPage.setTotal(page.getTotal());

        warnSettingPageVOIPage.setRecords(page.getRecords()

                .stream().map(
                        warnSetting -> {
                            DeviceWarnSettingPageVO pageVO = new DeviceWarnSettingPageVO();
                            pageVO.setDeviceName(deviceService.getById(warnSetting.getDeviceId()).getName());
                            pageVO.setLevel(warnSetting.getLevel());
                            if(new Integer(1).equals(warnSetting.getLevel())){
                                pageVO.setLevelStr("低");
                            }
                            if(new Integer(2).equals(warnSetting.getLevel())){
                                pageVO.setLevelStr("中");
                            }
                            if(new Integer(3).equals(warnSetting.getLevel())){
                                pageVO.setLevelStr("高");
                            }
                            if ("PM".equalsIgnoreCase(warnSetting.getType())) {
                                pageVO.setType("pm");
                            }
                            if ("TEMP".equalsIgnoreCase(warnSetting.getType())) {
                                pageVO.setType("温度");
                            }
                            if ("YW".equalsIgnoreCase(warnSetting.getType())) {
                                pageVO.setType("液位");
                            }
                            if ("DL".equalsIgnoreCase(warnSetting.getType())) {
                                pageVO.setType("电量");
                            }
                            if ("SIGNAL".equalsIgnoreCase(warnSetting.getType())) {
                                pageVO.setType("信号强度");
                            }
                            pageVO.setDownValue(warnSetting.getDownValue());
                            pageVO.setUpValue(warnSetting.getUpValue());
                            pageVO.setNoticeMsg(warnSetting.getNoticeMsg());
                            pageVO.setNoticeGap(warnSetting.getWarnGap());
                            pageVO.setId(warnSetting.getId());
                            pageVO.setDeviceId(warnSetting.getDeviceId());
                            return pageVO;
                        }
                ).collect(Collectors.toList()));

        return PageResult.success(warnSettingPageVOIPage);
    }


}
